package com.ruoyi.system.utils;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.system.domain.OperationLog;
import com.ruoyi.system.enums.BusinessTypeEnum;
import com.ruoyi.system.enums.OperateActionEnum;
import com.ruoyi.system.service.IOperationLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 操作日志工具类
 */
@Component
public class OperationLogUtils {

    private static IOperationLogService operationLogService;

    @Autowired
    public OperationLogUtils(IOperationLogService service) {
        operationLogService = service;
    }


    /**
     * 记录变更日志
     *
     * @param id
     * @param name
     * @param businessType
     * @param operateAction
     * @param beforeData
     * @param afterData
     * @param remark
     */
    public static void recordDataChange(
            String id,
            String name,
            String businessType,
            String operateAction,
            String beforeData,
            String afterData,
            String remark) {

        OperationLog log = new OperationLog();
        log.setBusinessType(businessType);
        log.setBusinessId(id);
        log.setBusinessName(name);
        log.setOperateAction(operateAction);
        log.setBeforeStatus(beforeData);
        log.setAfterStatus(afterData);
        log.setRemark(remark);

        operationLogService.saveOperationLog(log);
    }

    /**
     * 记录设计状态变更日志
     *
     * @param designId     设计ID
     * @param designName   设计名称
     * @param beforeStatus 变更前状态
     * @param afterStatus  变更后状态
     * @param remark       备注
     */
    public static void recordDesignStatusChange(
            Long designId,
            String designName,
            String beforeStatus,
            String afterStatus,
            String remark) {

        OperationLog log = new OperationLog();
        log.setBusinessType(BusinessTypeEnum.DESIGN.getCode());
        log.setBusinessId(designId.toString());
        log.setBusinessName(designName);
        log.setOperateAction(OperateActionEnum.STATUS_CHANGE.getCode());
        log.setBeforeStatus(beforeStatus);
        log.setAfterStatus(afterStatus);
        log.setRemark(remark);

        operationLogService.saveOperationLog(log);
    }

    /**
     * 记录设计方案新增日志
     *
     * @param schemeId   方案ID
     * @param schemeName 方案名称
     * @param designId   关联设计ID
     * @param remark     备注
     */
    public static void recordSchemeAdd(
            Long schemeId,
            String schemeName,
            Long designId,
            String remark) {

        OperationLog log = new OperationLog();
        log.setBusinessType(BusinessTypeEnum.DESIGN.getCode());
        log.setBusinessId(schemeId.toString());
        log.setBusinessName("设计方案：" + schemeName + "（设计ID：" + designId + "）");
        log.setOperateAction(OperateActionEnum.DATA_ADD.getCode());
        log.setRemark(remark);

        operationLogService.saveOperationLog(log);
    }

    /**
     * 记录设计方案修改日志
     *
     * @param schemeId   方案ID
     * @param schemeName 方案名称
     * @param remark     备注
     */
    public static void recordSchemeEdit(
            Long schemeId,
            String schemeName,
            String remark) {

        OperationLog log = new OperationLog();
        log.setBusinessType(BusinessTypeEnum.DESIGN.getCode());
        log.setBusinessId(schemeId.toString());
        log.setBusinessName("设计方案：" + schemeName);
        log.setOperateAction(OperateActionEnum.DATA_EDIT.getCode());
        log.setRemark(remark);

        operationLogService.saveOperationLog(log);
    }

    /**
     * 记录设计方案删除日志
     *
     * @param schemeId   方案ID
     * @param schemeName 方案名称
     * @param remark     备注
     */
    public static void recordSchemeDelete(
            Long schemeId,
            String schemeName,
            String remark) {

        OperationLog log = new OperationLog();
        log.setBusinessType(BusinessTypeEnum.DESIGN.getCode());
        log.setBusinessId(schemeId.toString());
        log.setBusinessName("设计方案：" + schemeName);
        log.setOperateAction(OperateActionEnum.DATA_DELETE.getCode());
        log.setRemark(remark);

        operationLogService.saveOperationLog(log);
    }

    /**
     * 记录设计图片上传日志
     *
     * @param imageId   图片ID
     * @param fileName  文件名
     * @param schemeId  方案ID
     * @param spaceType 空间类型
     * @param remark    备注
     */
    public static void recordImageUpload(
            Long imageId,
            String fileName,
            Long schemeId,
            String spaceType,
            String remark) {

        OperationLog log = new OperationLog();
        log.setBusinessType(BusinessTypeEnum.DESIGN.getCode());
        log.setBusinessId(imageId.toString());
        log.setBusinessName("设计图片：" + fileName + "（方案ID：" + schemeId + "，空间：" + spaceType + "）");
        log.setOperateAction(OperateActionEnum.FILE_UPLOAD.getCode());
        log.setRemark(remark);

        operationLogService.saveOperationLog(log);
    }

    /**
     * 记录设计图片删除日志
     *
     * @param imageId  图片ID
     * @param fileName 文件名
     * @param schemeId 方案ID
     * @param remark   备注
     */
    public static void recordImageDelete(
            Long imageId,
            String fileName,
            Long schemeId,
            String remark) {

        OperationLog log = new OperationLog();
        log.setBusinessType(BusinessTypeEnum.DESIGN.getCode());
        log.setBusinessId(imageId.toString());
        log.setBusinessName("设计图片：" + fileName + "（方案ID：" + schemeId + "）");
        log.setOperateAction(OperateActionEnum.FILE_DELETE.getCode());
        log.setRemark(remark);
        operationLogService.saveOperationLog(log);
    }


    /**
     * 获取最新日志
     *
     * @param businessId
     * @param businessType
     * @param operateAction
     * @return
     */
    public static OperationLog getBestNewLog(String businessId, String businessType, String operateAction) {
        // 2. 构建查询条件（LambdaQueryWrapper 避免硬编码字段名）
        LambdaQueryWrapper<OperationLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OperationLog::getBusinessType, businessType);
        queryWrapper.eq(OperationLog::getOperateAction, operateAction);
        queryWrapper.eq(OperationLog::getBusinessId, businessId);
        queryWrapper.orderByDesc(OperationLog::getOperateTime);
        queryWrapper.last("limit 1");
        return operationLogService.getOne(queryWrapper);
    }
}
